var notesClass = {};

notesClass.INSTANCE = "notes01.txt";

var OP_SAVE = "save";
var OP_REFRESH = "refresh";
var Q_INSTANCE = "name";

notesClass.Object = function(hostUrl) {
  this.REST_Path = hostUrl + "notes";
  this.Repository_Path = hostUrl + "synchronizer/";    
  this.contextPath = "/eva";

  this.instanceName = document.getElementById("instance").innerHTML;  
};


notesClass.Object.prototype.clearNotes = function (){
	document.getElementById("notes").value = "";
	// var status = document.getElementById("status");
	// status.style.color = color;
	// status.style.visibility = "visible";
	// status.innerHTML = message;
}

notesClass.Object.prototype.getNotes = function (){
  var message = document.getElementById('status');
  message.innerHTML = "";
  var time = new Date();
  var xhrArgs = {
            url: this.REST_Path,
            handleAs: "text",
            preventCache: true,
            content: {
                "operation": OP_REFRESH,
                "name": this.instanceName,
				"timeStamp": time.getTime()
            },
            load: function(resp) {
				data = resp.data;
				if (!data)
					return;
				if (data == "Failed" || !(data.indexOf('{"content":') == 0) ){
					message.innerHTML = data;
					return;
				}
				var res = eval('(' + resp.data + ')');
				var cont = res.content
				if (cont == null) {
					return;
				}
				note = document.getElementById("notes");
				note.value = cont;
				message.innerHTML = "Osvjezeno.";
				setTimeout("document.getElementById('status').innerHTML = ''", 3000);				
				return;
            },
            error: function(error) {
                message.innerHTML = "An unexpected error occurred: " + error;
            }
    }

  //Call the asynchronous xhrGet
  //var deferred = dojo.xhrPost(xhrArgs);
  sendAjax(xhrArgs);
  return;
}

notesClass.Object.prototype.postNotes = function (){
	note = document.getElementById("notes").value;
//	makeRequest("post", "?note=" + raw(notes));
  var message = document.getElementById('status');
  message.innerHTML = "";
  var time = new Date();
  var xhrArgs = {
            url: this.REST_Path,
            handleAs: "text",
            preventCache: true,
            content: {
                "operation": OP_SAVE,
                "name": this.instanceName,                
				"note": note,
				"timeStamp": time.getTime()
            },
            load: function(resp) {
				data = resp.data;
				if (data != "OK")
					return;
				message.innerHTML = "Uspjesno spremljeno.";
				setTimeout("document.getElementById('status').innerHTML = ''", 3000);
				return;
            },
            error: function(error) {
                message.innerHTML = "An unexpected error occurred: " + error;
            }
    }

  //Call the asynchronous xhrGet
  //var deferred = dojo.xhrPost(xhrArgs);
  sendAjax(xhrArgs);
  return;

}

function raw(str){
	return str.replace(/\t/g,'\\t').replace(/\r\n/g,'\\r\\n').replace(/\r/g,'\\r').replace(/\n/g,'\\n');
}

sendAjax = function(xhrArgs){
  if(gadgets.io !=null){
	var params = {};
	params[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.TEXT;
	var url =  xhrArgs.url +"?";
	i = 0;
	for(var key in xhrArgs.content) {
		if (i) url += '&';
		url += key + "=" + xhrArgs.content[key];
		i++;
	}
	gadgets.io.makeRequest(url, xhrArgs.load, params);
  }
}